package com.gxa.servlet.goods;

import com.alibaba.fastjson.JSON;
import com.gxa.pojo.Result;
import com.gxa.pojo.goods.GoodsPage;
import com.gxa.pojo.goods.Type;
import com.gxa.service.goods.GoodsOneTypeService;
import com.gxa.service.goods.GoodsTwoTypeService;
import com.gxa.service.goods.GoodsTypeService;
import com.gxa.service.goods.impl.GoodsOneTypeServiceimpl;
import com.gxa.service.goods.impl.GoodsTwoTypeServiceimpl;
import com.gxa.service.goods.impl.GoodsTypeServiceimpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = "/GoodsTypeServlet")
public class GoodsTypeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String begin = request.getParameter("page");
        String size = request.getParameter("size");
        int start = Integer.parseInt(begin);
        int size1 = Integer.parseInt(size);
        BufferedReader reader = request.getReader();
        StringBuilder stringBuilder = new StringBuilder();
        String sr;
        while ((sr=reader.readLine())!=null){
            stringBuilder.append(sr);
        }
        String s1 = stringBuilder.toString();
        Type type = JSON.parseObject(s1, Type.class);
        GoodsTypeService goodsTypeService = new GoodsTypeServiceimpl();
        List<Type> select = goodsTypeService.select(start,size1,type);
        int i = goodsTypeService.selectallPage();
        GoodsPage<Type> goodsPage = new GoodsPage<Type>();
        goodsPage.setAllconut(i);
        goodsPage.setRows(select);
        Map<String, Object> map = new HashMap<>();
        if(select!=null){
            map.put("Result", Result.ok("查询成功",goodsPage));
        }else {
            map.put("Result", Result.error());
        }
        String s = JSON.toJSONString(map);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(s);



    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
